// pages/my/question_answer/question_answer.js
import {
  QUESTION_LIST
} from "../../../request/api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: {
      loadStatus: 2,
    },
    dataList: [],
    pageToken: '',
    userId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app = getApp();
    this.data.userId = app.globalData.userId;
    this.loadMore();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  toDetail: function(e){
    var id = e.currentTarget.dataset.id;
    var url = '/pages/article/detail/detail?id='+id+'&type='+2;
    wx.navigateTo({
      url: url,
    });
  },

  refresh: function () {
    this.data.dataList = [];
    this.data.pageToken = '';
    this.loadMore();
  },

  loadMore: function () {
    QUESTION_LIST({
      'authorId': this.data.userId,
      'pageToken': this.data.pageToken,
    }).then(res => {
      if (res.data.code == 1) {
        var result = res.data.result;
        var list = result.items;
        if (this.data.pageToken) {
          list = this.data.dataList.concat(list);
        }
        this.setData({
          dataList: list,
          pageToken: result.nextPageToken,
          listData: {
            loadStatus: result.requestCount > result.responseCount ? 3 : 2,
          }
        });
      } else {
        this.setLoadStatus(2);
      }
    }).catch(err => {
      this.setLoadStatus(2);
    });
  },

  setLoadStatus: function (status) {
    this.setData({
      listData: {
        loadStatus: status,
      }
    });
  }
})